﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MigraDoc.DocumentObjectModel;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Utils;
using Payroll.Common.Utils;
using PayrollHR.Web;
using Payroll.BusinessLogic.Devices;

namespace PayrollHR.Administration.Modules
{
    public partial class VoucherAddControl : BasePayrollAdministrationUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.BindVoucher();
            }
        }
        protected int VoucherID
        {
            get
            {
                return CommonHelper.QueryStringInt("VoucherID");
            }
        }
        protected void BindVoucher()
        {
            if (this.VoucherID == 0)
            {
                ctrlVoucherInfo.FillDropDowns(null);
                return;
            }

            var voucher = this.VoucherService.GetVoucher(this.VoucherID);
            if (CommonHelper.IsNull(voucher))
                return;

            ctrlVoucherInfo.FillDropDowns(voucher.Vendor, voucher.OwnerId, voucher.PacketId);
            ctrlVoucherInfo.BindVoucher(voucher);
           
        }
        protected Voucher Save()
        {
            Voucher voucher = ctrlVoucherInfo.GetVoucher();
            voucher.VoucherID = this.VoucherService.Save(voucher);
            return voucher;
        }
        protected void SaveButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    this.Save();
                    Response.Redirect("Vouchers.aspx");
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }
        protected void SaveAndStayButton_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    var voucher = this.Save();
                    Response.Redirect(String.Format("VoucherAdd.aspx?VoucherID={0}", voucher.VoucherID));
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
                finally
                {
                    SaveButton.Enabled = true;
                    SaveAndStayButton.Enabled = true;
                }
            }
        }
        
        protected void Cancel_Click(object sender, EventArgs e)
        {
            try {
                Response.Redirect(String.Format("VoucherAdd.aspx?VoucherID={0}", 0));
            }
            catch (Exception exc)
            {
                ProcessException(exc);
            }
        }
    }
}